<template>
  <el-dialog title="分配权限" :visible="showDialog" @close="close">
    <el-tree
      ref="tree"
      :data="permList"
      node-key="id"
      check-strictly
      show-checkbox
      default-expand-all
      :props="{label:'title'}"
    />
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { permission } from '@/api/authority'
import { listToTree } from '@/utils/index'
import { getRoleInfoById, editRole } from '@/api/setting'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      permList: [],
      permIds: [],
      curPermId: ''
    }
  },
  async created() {
    // 获取权限列表
    const res = await permission({
      _limit: 999,
      _start: 0
    })
    this.permList = listToTree(res, '0')
  },
  methods: {
    // 设置权限
    async submit() {
      this.permIds = this.$refs.tree.getCheckedKeys()
      await editRole({
        id: this.curPermId,
        permissions: this.permIds
      })
      this.close()
    },
    // 获取角色权限点
    async getRolePermId(id) {
      this.curPermId = id
      const res = await getRoleInfoById(id)
      console.log(61, res)
      this.permIds = res.permissions.map(item => item.id)
      this.$refs.tree.setCheckedKeys(this.permIds)
    },
    close() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
